package cn.datawin.bms.modules.provider.controller;

import cn.datawin.bms.modules.provider.service.ProvProviderChannelService;
import cn.datawin.bms.provider.entity.ProvCorporateEntity;
import cn.datawin.bms.provider.entity.ProvProviderChannelEntity;
import cn.datawin.common.core.util.R;
import cn.datawin.common.log.annotation.SysLog;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 供应商通道
 *
 * @author datawin-sms
 * @date 2025-08-22 11:24:59
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/provider/providerChannel" )
@Tag(description = "providerChannel" , name = "供应商通道管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ProvProviderChannelController {

    private final ProvProviderChannelService provProviderChannelService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param provProviderChannel 供应商通道
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('provider_providerChannel_view')" )
    public R getProvProviderChannelPage(@ParameterObject Page page, @ParameterObject ProvProviderChannelEntity provProviderChannel) {
        LambdaQueryWrapper<ProvProviderChannelEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(provProviderChannel.getChannelName()),ProvProviderChannelEntity::getChannelName,provProviderChannel.getChannelName());
		wrapper.eq(Objects.nonNull(provProviderChannel.getStatus()),ProvProviderChannelEntity::getStatus,provProviderChannel.getStatus());
        return R.ok(provProviderChannelService.page(page, wrapper));
    }


	/**
	 * 获取所有上游供应商通道
	 */
	@Operation(summary = "获取所有上游供应商通道" , description = "获取所有上游供应商通道" )
	@GetMapping("/listAll" )
	@PreAuthorize("isAuthenticated()")  // 只需要登录权限
	public R getAllProvProviderChannel() {
		LambdaQueryWrapper<ProvProviderChannelEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(ProvProviderChannelEntity::getStatus,1);
		return R.ok(provProviderChannelService.list(wrapper));
	}

    /**
     * 通过id查询供应商通道
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('provider_providerChannel_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(provProviderChannelService.getById(id));
    }

    /**
     * 新增供应商通道
     * @param provProviderChannel 供应商通道
     * @return R
     */
    @Operation(summary = "新增供应商通道" , description = "新增供应商通道" )
    @SysLog("新增供应商通道" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('provider_providerChannel_add')" )
    public R save(@RequestBody ProvProviderChannelEntity provProviderChannel) {
        return R.ok(provProviderChannelService.saveChannel(provProviderChannel));
    }

    /**
     * 修改供应商通道
     * @param provProviderChannel 供应商通道
     * @return R
     */
    @Operation(summary = "修改供应商通道" , description = "修改供应商通道" )
    @SysLog("修改供应商通道" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('provider_providerChannel_edit')" )
    public R updateById(@RequestBody ProvProviderChannelEntity provProviderChannel) {
        return R.ok(provProviderChannelService.updateChannel(provProviderChannel));
    }

    /**
     * 通过id删除供应商通道
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除供应商通道" , description = "通过id删除供应商通道" )
    @SysLog("通过id删除供应商通道" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('provider_providerChannel_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(provProviderChannelService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param provProviderChannel 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('provider_providerChannel_export')" )
    public List<ProvProviderChannelEntity> export(ProvProviderChannelEntity provProviderChannel,Long[] ids) {
        return provProviderChannelService.list(Wrappers.lambdaQuery(provProviderChannel).in(ArrayUtil.isNotEmpty(ids), ProvProviderChannelEntity::getId, ids));
    }
}
